import {
  bus
} from './index'
import {
  backStore
} from "./backStore.js"
import dsBridge from 'dsbridge'

export const bridge = {
  ScannerCode(callback) {
    let scanner = api.require("FNScanner")
    scanner.open({}, callback)
  },
  PreviewFile(options) {
    let superFile = api.require("superFile")
    api.download({
      url: options.url
    }, function(ret, err) {
      if (ret.state === 1) {
        superFile.open({
          path: ret.savePath
        })
      } else {
        alert('文件下载失败')
      }
    })
  },
  Play(url) {
    api.openVideo({
      url: url
    })
  },
  PreviewPdf(options) {
    let pdfReader = api.require("pdfReader")
    pdfReader.open({
      path: options.url,
      hidden: {
        print: false,
        export: true,
        bookmark: true,
        email: true
      }
    })
  },
  /*
   * 绑定用户
   */
  pushBind(options) {
    let push = api.require('push');
    push.bind({
      userName: options.userName,
      userId: options.userId
    }, function(ret, err) {
      if (ret.status) {
        api.alert({
          msg: '绑定成功'
        });
      } else {
        api.alert({
          msg: err.msg
        });
      }
    });
  },
  /*
   * 解绑用户
   */
  pushUnBind(options) {
    let push = api.require('push');
    push.unbind({
      userName: options.userName,
      userId: options.userId
    }, function(ret, err) {
      if (ret.status) {
        api.alert({
          msg: '解除绑定成功'
        });
      } else {
        api.alert({
          msg: err.msg
        });
      }
    });
  },
  Live(url, callback) {
    dsBridge.call('live', {
      url
    }, callback)
  },
  PlayLive(url) {
    dsBridge.call('playLive', {
      url
    })
  }
}
